package org.meetunexpectedly.controller.middleground;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.meetunexpectedly.authority.Authority;
import org.meetunexpectedly.constant.AuditStatus;
import org.meetunexpectedly.entity.middleground.VideoStatistics;
import org.meetunexpectedly.entity.video.Video;
import org.meetunexpectedly.entity.vo.BasePage;
import org.meetunexpectedly.entity.vo.Result;
import org.meetunexpectedly.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 视频管理控制器
 * @Author: pupil
 * @Date: 2024/09/05 下午 2:37
 */
@RestController
@RequestMapping("/admin/video")
public class AdminVideoController {

    @Autowired
    private VideoService videoService;


    /**
     * 根据视频id获取视频
     *
     * @param id 视频id
     * @return
     */
    @GetMapping("/{id}")
    @Authority("admin:video:get")
    public Result getVideoById(@PathVariable Long id) {
        return Result.ok().data(videoService.getVideoById(id, null));
    }


    /**
     * 分页模糊查询视频
     *
     * @param basePage 分页信息
     * @param YV       视频号
     * @param title    视频标题
     * @return
     */
    @GetMapping("/page")
    @Authority("admin:video:page")
    public Result getVideoListByPage(BasePage basePage, @RequestParam(required = false) String YV, @RequestParam(required = false) String title) {
        IPage<Video> page = videoService.getVideoListByPage(basePage, YV, title);
        if (ObjectUtils.isEmpty(page)) {
            return Result.ok();
        }
        return Result.ok().data(page.getRecords()).count(page.getTotal());
    }

    /**
     * 删除视频
     *
     * @param id 视频id
     * @return
     */
    @DeleteMapping("/{id}")
    @Authority("admin:video:delete")
    public Result deleteVideo(@PathVariable Long id) {
        if (!videoService.deleteVideo(id)){
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }


    /**
     * 放行视频
     * @param video 视频
     * @return
     */
    @PostMapping("/audit")
    @Authority("admin:video:audit")
    public Result auditVideo(@RequestBody Video video){
        videoService.auditProcess(video);
        return Result.ok().message("审核放行");
    }

    /**
     * 下架违规视频
     * @param id 视频id
     * @return
     */
    @PostMapping("/violations/{id}")
    @Authority("admin:video:violate")
    public Result videoViolate(@PathVariable Long id){
        if(!videoService.videoViolate(id)){
            return Result.error().message("下架失败");
        }
        return Result.ok().message("下架成功");
    }

    /**
     * 视频数据统计
     * @return
     */
    @GetMapping("/statistics")
    @Authority("admin:video:statistics")
    public Result videoStatistics(){
        VideoStatistics videoStatistics = new VideoStatistics();
        int allCount = videoService.count(new LambdaQueryWrapper<Video>());
        int successCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.SUCCESS.num));
        int processCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PROCESS.num));
        int passCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PASS.num));
        int manualCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.MANUAL.num));
        videoStatistics.setAllCount(allCount);
        videoStatistics.setSuccessCount(successCount);
        videoStatistics.setProcessCount(processCount);
        videoStatistics.setPassCount(passCount);
        videoStatistics.setManualCount(manualCount);

        return Result.ok().data(videoStatistics);
    }
}
